package org.darcy.framework.model;

import java.io.Serializable;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@ApiModel("分页查询参数")
@Getter
@Setter
public class PageParam implements Serializable {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "页码")
	protected int p = 1;

	@ApiModelProperty(value = " 每页几条记录")
	protected int pageSize = 10;

	@ApiModelProperty(value = "总记录数 ")
	protected int totalCount;

	@ApiModelProperty(value = "总页数")
	protected int totalPage;

	@ApiModelProperty(value = "模糊查询参数")
	private String keywords;

	@ApiModelProperty(value = "是否有上一页")
	protected boolean hasPreviousPage;

	@ApiModelProperty(value = "上一页")
	protected int prePage;

	@ApiModelProperty(value = "是否有下一页")
	protected boolean hasNextPage;

	@ApiModelProperty(value = "下一页")
	protected int nextPage;

	/**
	 * mybatis plus 的分页查询参数，页码和每页记录数
	 */
	public IPage buildMybatisPlusPageParam() {
		return new Page(this.getP(), this.getPageSize(), this.getTotalCount());
	}

	/**
	 * 根据 mybatis plus 的分页查询参数，获取自定义分页参数
	 */
	public static PageParam getParam(Page data) {
		PageParam p = new PageParam();
		p.setP((int) data.getCurrent());
		p.setPageSize((int) data.getSize());
		p.setTotalCount((int) data.getTotal());
		int totalpage = p.getTotalCount() % p.getPageSize() == 0 ? p.getTotalCount() / p.getPageSize()
				: 1 + p.getTotalCount() / p.getPageSize();
		p.setTotalPage(totalpage);
		if (p.getP() > 1) {
			p.setHasPreviousPage(true);
			p.setPrePage(p.getP() - 1);
		} else {
			p.setHasPreviousPage(false);
		}
		if (p.getP() < p.getTotalPage()) {
			p.setHasNextPage(true);
			p.setNextPage(p.getP() + 1);
		} else {
			p.setHasNextPage(false);
		}
		return p;
	}

}
